package com.ksm.zyz.common.util;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.ss.formula.functions.Now;

import com.ksm.zyz.common.constant.GlobalConstant;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

public class DateUtil {
	
	public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
		"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
		"t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
		"6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
		"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
		"W", "X", "Y", "Z" };


	/**
	 * 获取8位uuid
	 * @return
	 */
	public static String getShortUuid() {
		StringBuffer shortBuffer = new StringBuffer();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		for (int i = 0; i < 8; i++) {
			String str = uuid.substring(i * 4, i * 4 + 4);
			int x = Integer.parseInt(str, 16);
			shortBuffer.append(chars[x % 0x3E]);
		}
		return shortBuffer.toString();
	}
	/**
	 * 时间序列(15位数)
	 * @return
	 */
	public static String getUUID(){
    	SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");
    	Date date = new Date();
        //格式化日期  
        StringBuffer datestr = new StringBuffer(format.format(date));
        Random random = new Random();
        int number = random.nextInt(999)%(999-100+1) + 100;
        return datestr.append(number).toString();
     }
	
	/** 返回当前时间(yyyy-MM-dd HH:mm:ss) */
	public static String dataNow() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String now = String.valueOf(df.format(new Date()));
		return now;
	}
	
	/** 返回当前时间(yyMMddHHmmss) */
	public static String dataNowShot() {
		SimpleDateFormat df = new SimpleDateFormat("yyMMddHHmmss");
		return String.valueOf(df.format(new Date()));
	}
	
	public static String dateDifference(String bDate,String sDate) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
		Date nTime = sdf.parse(bDate);
		Date sTime = sdf.parse(sDate);
		long l = nTime.getTime()-sTime.getTime();
		long sec = l/1000;
		long time = 3600;
		sec = time -sec;
		return sec+"";
	}
	
	/**
	 * 计算两个时间差是否大于一小时
	 * @param startDate
	 * @param nowDate
	 * @return
	 */
	public static boolean substractionDateGTROneHours(Date startDate,Date nowDate){
		long nd = 1000 * 24 * 60 * 60;
	    long nh = 1000 * 60 * 60;
	    // long ns = 1000;
	    long diff = nowDate.getTime() - startDate.getTime();
	    long day = diff / nd;
	    long hour = diff % nd / nh;
	    if (day > 0) {
			return true;
		}else if (hour >= 1) {
			return true;
		}else{
			return false;
		}
	}
	
	//测试聊天5分钟
	public static boolean substractionDateGTRFiveMinutes(Date startDate,Date nowDate){
		long nd = 1000 * 24 * 60 * 60;	//秒/天
	    long nh = 1000 * 60 * 60;		//秒/小时
	    long nm = 1000 * 60;			//秒/分钟
	    long diff = nowDate.getTime() - startDate.getTime();
	    long day = diff / nd;
	    long hour = diff % nd / nh;
	    long min = diff % nd % nh / nm;
	    if (day > 0) {
			return true;
		}else if (hour > 0) {
			return true;
		}else if( min >= 5){
			return true;
		}else{
			return false;
		}
	}
	
	/** 返回当前日期 */
	public static String today() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String now = String.valueOf(df.format(new Date()));
		return now.substring(0, 10);
	}
	/** 返回当前年(YYYY)*/
	public static String getyear() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String now = String.valueOf(df.format(new Date()));
		now = now.substring(0, 4);
		return now;
	}
	/** 返回当前月 */
	public static String getmonth() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String now = String.valueOf(df.format(new Date()));
		now = now.substring(5, 7);
		return now;
	}
	/** 返回当前年*/
	public String getyear_month() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String now = String.valueOf(df.format(new Date()));
		now = now.substring(0, 7);
		return now;
	}
	/** 返回当前日期 */
	public static String getday() {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHH:mm:ss");
		String now = String.valueOf(df.format(new Date()));
		return now;
	}

	/**
	 * 获取日期A和B之间所有日期
	 * @param p_start
	 * @param p_end
	 * @return
	 */
	public static List<Date> getDates(Calendar p_start, Calendar p_end) {
        List<Date> result = new ArrayList<Date>();
        Calendar temp = p_start.getInstance();
        temp.add(Calendar.DAY_OF_YEAR, 1);
        while (temp.before(p_end)) {
            result.add(temp.getTime());
            temp.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }
	
	public static String longToDate(String time){
		Date date = new Date(Long.valueOf(time));
		return DateFormatUtils.format(date,"yyyy-MM-dd HH:mm:ss");
	}

	public static String jenerateSqlInCondition(String[] queryArray){
		return joinArray(queryArray,"','");
	}
	
    public static String joinArrayList(List<String> array, String join){
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<array.size();i++){
             if(i==(array.size()-1)){
                 sb.append(array.get(i));
             }else{
                 sb.append(array.get(i)).append(join);
             }
        } 
        return new String(sb);
    }
    
    public static String joinArray(String[] array, String join){
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<array.length;i++){
             if(i == (array.length - 1)){
                 sb.append(array[i]);
             }else{
                 sb.append(array[i]).append(join);
             }
        } 
        return new String(sb);
    }
    
    //生成订单号
    public static String creatOrderNumber(int orderType){
    	int max=99999;
        int min=10000;
        Random random = new Random();
    	SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmssSSS");
    	Date date = new Date();
        //格式化日期  
        StringBuffer datestr = new StringBuffer();
        int s = random.nextInt(max)%(max - min + 1) + min;
        if (orderType == GlobalConstant.TALK_ORDER) {
			datestr.append(GlobalConstant.TALK_S_ORDER);
		}else if (orderType == GlobalConstant.ZHILIAO_ORDER) {
			datestr.append(GlobalConstant.ZHILIAO_S_ORDER);
		}else if (orderType == GlobalConstant.TALKCARD_ORDER) {
			datestr.append(GlobalConstant.TALKCARD_S_ORDER);
		}else{
			return null;
		}
        datestr.append(sdf.format(date));
        return datestr.append(s).toString();
    }

    //生成商户订单流水号，该值在商户端应保持唯一（可自定义格式规范）
    public static String getOutTradeNo() {
    	int max = 99999;
        int min = 10000;
		SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmssSSS");
		Date date = new Date();
		String key = format.format(date);
		Random r = new Random();
		int s = r.nextInt(max)%(max-min+1)+min;
		key = key + s;
		return key;
	}
	
	public static void main(String[] args) throws ParseException {
		String result = dateDifference(dataNow(),"2016-09-20 17:00:00");
		
		System.out.println(result);
		
	}
	
}


